package com.feituan.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feituan.core.persistence.mapper.EquipmentShopMapper;
import com.feituan.core.persistence.mapper.MerchantMapper;
import com.feituan.core.pojo.entity.EquipmentShop;
import com.feituan.core.pojo.entity.Merchant;
import com.feituan.core.pojo.request.EquipmentShopPageReq;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.service.IEquipmentShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EquipmentShopServiceImpl extends ServiceImpl<EquipmentShopMapper, EquipmentShop> implements IEquipmentShopService {
    @Autowired
    private EquipmentShopMapper equipmentShopMapper;

    @Autowired
    private MerchantMapper merchantMapper;
    /**
     * 添加
     * @param equipmentShop equipmentShop
     */
    @Override
    public boolean save(EquipmentShop equipmentShop) {
        if (equipmentShop.getId() != null) {
            return this.equipmentShopMapper.updateById(equipmentShop) > 0;
        }
        return this.equipmentShopMapper.insert(equipmentShop) > 0;
    }


    /**
     * 更新
     * @param equipmentShop equipmentShop
     */
    @Override
    public boolean update(EquipmentShop equipmentShop) {
        return this.equipmentShopMapper.updateById(equipmentShop) > 0;
    }


    /**
     * 分页查询
     * @param request request
     */
    @Override
    public PageData<EquipmentShop> queryPage(EquipmentShopPageReq request) {
        Page<EquipmentShop> page = this.lambdaQuery()
                .page(new Page<>(request.getPage(), request.getLimit()));
        List<EquipmentShop> records = page.getRecords();
        for (EquipmentShop record : records) {
            Merchant merchant = merchantMapper.selectById(record.getShopId());
            record.setShopName(merchant.getName());
        }
        return PageData.build(page.getCurrent(), page.getTotal(), page.getRecords());
    }

    /**
     * 查询所有
     */
    @Override
    public List<EquipmentShop> selectAll() {
        QueryWrapper<EquipmentShop> objectQueryWrapper = new QueryWrapper<>();
        return this.equipmentShopMapper.selectList(objectQueryWrapper);
    }

    /**
     * 删除
     * @param id id
     */
    @Override
    public boolean delete(Long id) {
        return this.equipmentShopMapper.deleteById(id) > 0;
    }

    /**
     * 根据type和shopId查询
     * @param type type
     * @param shopId shopId
     */
    @Override
    public List<EquipmentShop> selectByTypeAndShopId(String type, Long shopId) {
        return this.equipmentShopMapper.selectByTypeAndShopId(type, shopId);
    }

    /**
     * 根据shopId查询
     * @param shopId shopId
     */
    @Override
    public List<EquipmentShop> selectByShopId(Long shopId) {
        return this.equipmentShopMapper.selectByShopId(shopId);
    }
}
